﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');

FantastPlatform.UI.PersonRoleForm = function (config) {
    Ext.apply(this, config);
    var newWidth = this.width;
    var newHeight = this.height;
    var thispoliceNum = this.policemanNumber;
    var grid1 = new FantastPlatform.UI.UserRoleGrid({
        url: '../Service/PersonRoleService.aspx?Action=SearchAddUser&RoleId=' + this.roleId,
        height: newHeight / 2,
        tbar: [
            {
                text: '移出角色已有的用户',
                iconCls: 'delete',
                handler: removeRole

            }, '-', {
                text: '刷新',
                iconCls: 'updata',
                handler: function () { grid1.onUpdataGrid(); }
            }
                    , '->',
                    {
                        tooltip: { text: '可以通过拖拽到下面的列表进行删除', title: '删除许可', autoHide: true },
                        iconCls: 'down'
                    }
            ]
        });
        grid1.onUpdataGrid();

        var grid2 = new FantastPlatform.UI.RoleForm({
            url: '../Service/PersonRoleService.aspx?Action=Search',
            height: newHeight / 2,
            tbar: [
            {
                text: '给角色添加新的用户',
                iconCls: 'add',
                handler: addRole
            }, '->',
            {
                tooltip: { text: '可以通过拖拽到上面的列表进行添加', title: '添加许可', autoHide: true },
                iconCls: 'up'
            }
        ]
        })
        grid2.onUpdataGrid();

        var u_tpl = new Ext.Template(
    '<p>角色名称: {name}</p>',
    '<p>角色编码: {code}</p>',
       '<p>角色描述:{description}</p>'
);

        var uRoleInfo = new Ext.Panel({
            border: false,
            height: newHeight / 2,
            style: 'border:solid 1px',
            title: '角色信息',
            split: true,
            bodyStyle: 'padding:5px 5px 0',
            collapsible: true,
            autoLoad: { url: '../Setting/URoleForm.aspx?Id=' + this.roleId, scripts: true }
        })

        var uSearchInfo = new Ext.form.FormPanel
    ({
        border: false,
        title: '查询条件',
        split: true,
        url: '../Service/PersonRoleService.aspx?Action=GetPolice',
        style: 'border:solid 1px',
        labelWidth: 50,
        bodyStyle: 'padding:5px 5px 0',
        height: newHeight / 2,
        defaultType: 'textfield',
        collapsible: true,
        defaults: { width: 120 },
        tbar: ['从警员列表中查询'],
        items: [{
            fieldLabel: '姓名',
            name: 'name'
        }, {
            fieldLabel: '警号',
            name: 'Code'
        }],
        buttons: [{
            text: '提交查询',
            handler: function () { onPoliceSearchClick(uSearchInfo, grid2, null); }
        }]
    });

        this.items = [{
            columnWidth: .7,
            width: 500,
            height: newHeight / 2,
            border: false,
            items: [grid1]
        }, {
            columnWidth: .3,
            width: 160,
            height: newHeight / 2,
            border: false,
            items: [uRoleInfo]
        }, {
            columnWidth: .7,
            width: 500,
            height: newHeight / 2,
            border: false,
            items: [grid2]
        }, {
            columnWidth: .3,
            width: 160,
            height: newHeight / 2,
            border: false,
            items: [uSearchInfo]
        }];

        var RoleId = this.roleId;
        function removeRole() {
            var node = checkGridSelected(grid1);
            if (node) {
                if (getIdFromGrid(node, 'Number') == thispoliceNum) {
                    Ext.Msg.alert('提示', '不能将自己从该角色表中移除');
                }
                else {
                    Ext.Msg.confirm('提示', '确认要移除角色列表中已有的角色？', function (btn) {
                        if (btn == 'yes') {
                            Ext.Msg.wait('正在移除负责街道中已有街道……', '移除');
                            var fIds = getIdFromGrid(node, 'Id');
                            ajaxCalled(null, '../Service/PersonRoleService.aspx?Action=RemoveUser&RoleId=' + RoleId, fIds, grid1, null);
                        }
                    });
                }
            }
        }

        function addRole() {
        var node = checkGridSelected(grid2);
        if (node) {
            Ext.Msg.confirm('提示', '确认要添加警员到角色表中？', function (btn) {
                if (btn == 'yes') {
                    Ext.Msg.wait('正在警员到角色中……', '添加');
                    var fIds = getIdFromGrid(node, 'Id');
                    //alert(fIds);
                    ajaxCalled(null, '../Service/PersonRoleService.aspx?Action=AddUser&RoleId=' + RoleId, fIds, grid1, null);
                }
            })
        }
    }

    function onPoliceSearchClick(form, grid, sender, fn) {
        if (form.form.isValid()) {
            var name = form.form.getValues().name;
            var Code = form.form.getValues().Code;
            grid2.store.removeAll();
            grid2.store.proxy.conn = { method: 'post', url: '../Service/PersonRoleService.aspx?Action=GetPolice' };
            grid2.store.load({ params: { start: 0, limit: grid2.paperBar.getPageNumber(), dir: '', sort: '', code: Code,name:name} });
        }
    }
    FantastPlatform.UI.PersonRoleForm.superclass.constructor.call(this, {
        layout: 'column',
        border: false
    });
};

Ext.extend(FantastPlatform.UI.PersonRoleForm, Ext.Panel, {

});